package controller;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import multiformat.*;
import views.*;

import javax.swing.*;

public class InputController extends JPanel implements ActionListener{
	private JTextField input;
	private JButton submit;
	Calculator calc;
	CalculationHistory history;
	
	public InputController(Calculator calc, CalculationHistory history){	
		this.calc = calc;
		this.history = history;
		this.setLayout(new FlowLayout());
		
		input = new JTextField();
		input.setColumns(12);
		
		submit = new JButton("Submit");
		

		input.addActionListener(this);
		submit.addActionListener(this);
		this.add(input);
		this.add(submit);
	}

	/**
	 * ActionPerformed, hier wordt geluisterd naar een action. Als er een action is uitgevoerd wordt deze hier afgehandeld.
	 * Gekopieerd en veranderd vanuit Command.
	 * @param e
	 */
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == submit){
			
			String command = input.getText();
			history.addString("\n" + command);
			
			if(command.equals("+")) calc.add();
		      else if(command.equals("-")) calc.subtract();
		      else if(command.equals("*")) calc.multiply();
		      else if(command.equals("/")) calc.divide();
		      else if(command.equals("dec")) calc.setBase(new DecimalBase());
		      else if(command.equals("bin")) calc.setBase(new BinaryBase());
		      else if(command.equals("hex")) calc.setBase(new HexBase());
		      else if(command.equals("oct")) calc.setBase(new OctBase());
		      else if(command.equals("rat")) calc.setFormat(new RationalFormat());
		      else if(command.equals("fixed")) calc.setFormat(new FixedPointFormat());
		      else if(command.equals("float")) calc.setFormat(new FloatingPointFormat());
		      else if(command.equals("del")) calc.delete();
		      else if(command.indexOf("op") >= 0) {
		    	  try{
		          	calc.addOperand(command.substring(2).trim());
		          }catch(FormatException ex){
		        	  history.addString("\nWrong operand: " + ex.getMessage());
		          }catch(NumberBaseException ex){
		        	  history.addString("\nWrong Base: " + ex.getMessage());
		          }
	        }else if(command.equals("help")){
	      	  printHelp();
	        }else{
	        	history.addString("Error! Not a valid command");
	        }
			input.setText("");
			history.addString("\n["+calc.getBase().getName()+","
                    + calc.getFormat().getName()+","
                    + calc.firstOperand() + ", "
                    + calc.secondOperand() + "] >");
		}
	}
	
	 /**
	  * Als deze methode wordt aangeroepen dan word de handleiding van het programma geprint.
	  */
	 void printHelp() {
		    history.addString("\n");
		    history.addString("\nInsert one of the following commands:");
		    history.addString(" \n op <numero>  (store an operand)");
		    history.addString(" \n+            (sum the last two operands)");
		    history.addString(" \n -            (substract the last operand from the previous one)");
		    history.addString(" \n *            (multiply the last two operands)");
		    history.addString(" \n /            (divide the last two operands)");
		    history.addString(" \n dec          (switch to base 10)");
		    history.addString(" \n bin          (switch to binary base)");
		    history.addString(" \n hex          (switch to hexadecimal base)");
		    history.addString(" \n fixed        (switch to fixed point format)");
		    history.addString(" \n float        (switch to floating point format)");
		    history.addString(" \n rat          (switch to rational format)");
		    history.addString(" \n del          (remove last operand)");
		    history.addString(" \n help         (print this command list)");
		    history.addString(" \n exit         (terminate execution)");
		    history.addString("\n");
	  }
		
}

